#include "Spawner.h"
#include "glm\glm.hpp"
#include "DisplayPrimitive.h"
#include "Entity.h"
#include "GameManager.h"

PlayerGameEntity * Spawner::SpawnPlayer( const PlayerGameEntityDef & inDef,
		GameManager * manager) {
		//Init player entity
		PlayerGameEntity * retVal = NULL;
		
		retVal = new PlayerGameEntity( inDef, manager ); //Passes in a PlayerGameEntityDef 

		//Initialize the weapon with the player.
		WeaponGameEntity * weap = CreateWeapon(inDef.startWeaponProps, manager);
		retVal->SetWeapon( weap );

		manager->mEntityList.push_back(weap);
	
		PhysicsDisplayPrimitiveDef physDef;
		physDef.prim = retVal->mPhysPrim;
		PhysicsDisplayPrimitive * physPrim = new PhysicsDisplayPrimitive(&physDef);

		manager->mDisplayManager->AddDisplayPrimitive(physPrim);
		manager->mPhysicsManager->AddPhysicsObject(retVal->mPhysPrim);

		return retVal;
}

ItemGameEntity * Spawner::SpawnItem( const ItemGameEntityDef & inDef,
	GameManager * inManager){
	ItemGameEntity * retVal = NULL;
	retVal = new ItemGameEntity(inDef, inManager);

	return retVal;
}

ProjectileGameEntity * Spawner::SpawnProjectile ( const ProjectileGameEntityDef & inDef,
	GameManager * inManager) {
		ProjectileGameEntity * retVal = NULL;
		retVal = new ProjectileGameEntity(inDef, inManager);


		inManager->mPhysicsManager->AddPhysicsObject(retVal->GetPhysicsPrimitive());
		inManager->mDisplayManager->AddDisplayPrimitive(retVal->GetDisplayPrimitive());

		b2Body * physBody = retVal->GetPhysicsPrimitive()->mBody;
		//XXX Use isdogravity from definition
		physBody->SetBullet(true); //Perform CCD
		if (!inDef.mDoGravity) {
			physBody->SetGravityScale(0);//No gravity
		}
		b2Vec2 vel;
		vel = b2Vec2(inDef.startTarget.x - inDef.worldPos.x, 
			inDef.startTarget.y - inDef.worldPos.y);
		vel.Normalize();
		vel.x = vel.x * inDef.speed;
		vel.y = vel.y * inDef.speed;
		physBody->SetLinearVelocity(vel);

		return retVal;
}